$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        table:null,
        currentKey:null,
        init:function () {
            var $this = this;
            this.table = $('#list');
            $('.xh-datagrid-toolbar').parseToolbar();
            xhExtra.disableButton($("#bt_list_del"),'eu');
            xhExtra.disableButton($("#bt_list_check"),'eu');
            xhExtra.disableButton($("#bt_list_unCheck"),'eu');
            xhExtra.disableButton($("#bt_list_stat"),'eu');
            xhExtra.disableButton($("#bt_list_teaAppStat"),'eu');
            this.table.treegrid({
                url: baseUrl+'bpm/operate_pro/getOperateProData',
                queryParams: {},
                onBeforeSelect:function (row) {
                    if (!$this.currentKey) {
                        return true;
                    } else  if ($this.currentKey === row['iId']) {
                        $this.currentKey = null;
                        $this.table.treegrid('unselect', row['iId']);
                        $this.table.treegrid('collapse', row['iId']);
                        xhExtra.disableButton($("#bt_list_del"),'eu');
                        xhExtra.disableButton($("#bt_list_check"),'eu');
                        xhExtra.disableButton($("#bt_list_unCheck"),'eu');
                        xhExtra.disableButton($("#bt_list_stat"),'eu');
                        xhExtra.disableButton($("#bt_list_teaAppStat"),'eu');
                        return false;
                    }
                },
                onSelect:function (row) {
                    $this.currentKey = row['iId'];
                    $this.table.treegrid('expand', row['iId']);
                    var arr = $this.currentKey.split('_');
                    if (arr.length === 1) {
                        xhExtra.enableButton($("#bt_list_del"),'eu');
                    } else {
                        xhExtra.disableButton($("#bt_list_del"),'eu');
                    }
                    xhExtra.enableButton($("#bt_list_check"),'eu');
                    xhExtra.enableButton($("#bt_list_unCheck"),'eu');
                    xhExtra.enableButton($("#bt_list_stat"),'eu');
                    xhExtra.enableButton($("#bt_list_teaAppStat"),'eu');
                },
                onDblClickRow:function (row) {
                    $this.funObj().openEditForm(row);
                }
            });

            this.listener();
        },
        listener:function () {
            var $this = this;
            var xh_dataGrid = $('.xh-datagrid');
            //添加
            $('#bt_list_add').click(function () {
                $this.funObj().openAddForm();
            });
            //确认
            $("#bt_list_check").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().checkNode();
            });
            //取消完成
            $("#bt_list_unCheck").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().unCheckNode();
            });
            //统计
            $("#bt_list_stat").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().statData();
            });
            //应用教师统计
            $("#bt_list_teaAppStat").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().teaAppStat();
            });
            //删除
            $("#bt_list_del").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDel();
            });
            //查看附件
            xh_dataGrid.on('click','.showFileTable',function () {
                var id = $(this).attr('iId');
                $this.funObj().showFileTable(id);
            });
            //搜索
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
        },
        funObj:function () {
            var $this = this;
            return {
                searchList: function () {
                    var params = {
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        }
                    };
                    $this.table.treegrid('reload',params);
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '添加运营项目',
                        content: xhTool.getFrameString('bpm/operate_pro/addOperatePro', null, 700),
                        size: 'md',
                        width: 800,
                        button:'确认',
                        backClose: false
                    });
                },
                askDel:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) {
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var iId = selection[0]['iId'];
                    var arr = iId.split('_');
                    if (arr.length === 1) {
                        xhExtra.askChangeStatus('bpm/operate_pro/delOperatePro',{id:iId},null,'确定要删除?',function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.table.treegrid('reload');
                        });
                    } else if (arr.length === 2) {
                        window.top.xhTop.showError('错误', '该节点不可删除');
                        return false;
                    }
                },
                statData:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) { //新增
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var iId = selection[0]['iId'];
                    var startDate = selection[0]['tStartDate'];
                    var endDate = selection[0]['tEndDate'];
                    if (!startDate) {
                        startDate = '';
                    }
                    if (!endDate) {
                        endDate = '';
                    }
                    var _schoolName = '';
                    xhExtra.ajaxRequest('bpm/operate_pro/getSchoolName',{iId:iId},{async: false},function (result) {
                        _schoolName = result.schoolName;
                    });
                    window.top.xhTop.showModal({
                        title: '统计应用数据：' + _schoolName,
                        content: xhTool.getFrameString('bpm/operate_pro/statAppData', {iId:iId, startDate:startDate, endDate:endDate}, 600),
                        size: 'lg',
                        backClose: false
                    });
                },
                teaAppStat:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) { //新增
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var iId = selection[0]['iId'];
                    var startDate = selection[0]['tStartDate'];
                    var endDate = selection[0]['tEndDate'];
                    if (!startDate) {
                        startDate = '';
                    }
                    if (!endDate) {
                        endDate = '';
                    }
                    var _schoolName = '';
                    xhExtra.ajaxRequest('bpm/operate_pro/getSchoolName',{iId:iId},{async: false},function (result) {
                        _schoolName = result.schoolName;
                    });
                    // window.top.xhTop.showModal({
                    //     title: '统计应用数据：' + _schoolName,
                    //     content: xhTool.getFrameString('bpm/operate_pro/statTeaAppData', {iId:iId, startDate:startDate, endDate:endDate}, $(window).height()),
                    //     size: 'lg',
                    //     backClose: false
                    // });
                    window.top.xhTop.createNewTab('教师应用数据：' + _schoolName, 'bpm/operate_pro/statTeaAppData?iId='+iId+'&startDate='+startDate+'&endDate='+endDate);
                },
                openEditForm:function (row) {
                    var depth = row['depth'];
                    var id = row['iId'];
                    window.top.xhTop.showModal({
                        title: '修改运营项目信息',
                        content: xhTool.getFrameString('bpm/operate_pro/modifyOperatePro', {id:id, depth:depth}, 600),
                        size: 'md',
                        button:'提交',
                        width: 800,
                        backClose: false
                    });
                },
                checkNode:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) {
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var iId = selection[0]['iId'];
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '完成',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写确认备注',
                        html: '<div><textarea type="text" id="remark" class="swal2-textarea" placeholder="选填"></textarea></div>' +
                        '<div align="left"><input name="resetDepth" type="checkbox" onclick="javascript:document.getElementById(\'resetDepth\').value=this.checked;">\n 将子任务全部置为完成状态</div>' +
                        '<div><input name="resetDepth" type="hidden" value="false" id="resetDepth">\n</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var remark     = $('#remark', window.top.document).val();
                                var resetDepth     = $('#resetDepth', window.top.document).val();
                                    xhExtra.ajaxRequest('bpm/operate_pro/checkOperatePro', {
                                        id:iId,
                                        remark:remark,
                                        resetDepth:resetDepth,
                                        type:1
                                    }, {success: true});
                                    resolve();
                                    $this.table.treegrid('reload',{openId:iId});
                                    if (iId.indexOf("_") === -1 ) {
                                        $this.table.treegrid('reload',{openId:iId});
                                    }
                            })
                        }
                    });
                },
                unCheckNode:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) {
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var iId = selection[0]['iId'];
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '完成',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写取消备注',
                        html: '<div><textarea type="text" id="remark" class="swal2-textarea" placeholder="选填"></textarea></div>' +
                        '<div align="left"><input name="resetDepth" type="checkbox" onclick="javascript:document.getElementById(\'resetDepth\').value=this.checked;">\n 将子任务全部置为未完成状态</div>' +
                        '<div><input name="resetDepth" type="hidden" value="false" id="resetDepth">\n</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var remark     = $('#remark', window.top.document).val();
                                var resetDepth     = $('#resetDepth', window.top.document).val();
                                xhExtra.ajaxRequest('bpm/operate_pro/checkOperatePro', {
                                    id:iId,
                                    remark:remark,
                                    resetDepth:resetDepth,
                                    type:2
                                }, {success: true});
                                resolve();
                                $this.table.treegrid('reload',{openId:iId});
                                if (iId.indexOf("_") === -1 ) {
                                    $this.table.treegrid('reload',{openId:iId});
                                }
                            })
                        }
                    });
                },
                showFileTable: function (id) {
                    if (!id){
                        window.top.xhTop.showError('id参数错误');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '查看附件',
                        content: xhTool.getFrameString('bpm/operate_pro/showFileTable', {id:id}, 350),
                        size: 'md',
                        width: 800,
                        backClose: false
                    });
                }
            }
        }
    }
};